<?php defined('SYSPATH') or die('No direct access allowed.');
//
/* 
 * 视图类，所有视图路径以kohana默认视图根目录为根目录
 * 用途：免父目录路径载入视图，
 * 前提：视图放置在以controller名称命名的目录下
 * 用法：
 * lib_view::parentdir($父目录路径);
 * lib_view::factory($视图名,$传入变量数组);
 * lib_view::reset();//重置已设置的父目录路径
 * */
class lib_view extends Kohana_View{
    
    private static $parent_dir = '';
    
    private static $dir = '';
    
    public static function parentdir($dir = null){
        if (is_string($dir)) self::$parent_dir = trim($dir,'\/\\').'/';
    }
    
    public static function reset(){
    	self::$parent_dir = '';
    	self::$dir = '';
    }
    
    private static function setdir(){
        $contoller = Kohana_Request::current()->controller();
        if (!empty($contoller)) self::$dir = $contoller.'/';
        if (!empty(self::$parent_dir)) self::$dir = self::$parent_dir.self::$dir;
    }
    
    public static function factory($view = NULL, array $data = NULL){
        self::setdir();
        if (!Kohana::find_file('views', $patch = self::$dir.$view)){
        	if (true === project::config('libconfig')->{'view not found then 404'}){
        		__404(new lib_exception('view not found:'.$patch));
        	}
            return false;
        }
        return new View($patch, $data);
        //return parent::factory($patch);
    }
}